﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhotoGallery.Abstract;
using PhotoGallery.Concrete;
using System.Web.Mvc;
using PhotoGallery.Entities;

namespace PhotoGallery.Controllers
{
    public class OwnersController : Controller
    {
         private IOwnersRepository OwnersRepository;



        public OwnersController()
        {
            
            string connString = @"server=.\SQLEXPRESS; Integrated Security=SSPI ;database=PhotoGallery;";
            OwnersRepository = new SqlOwnersRepository(connString);
        }


        [HttpGet]
        public ViewResult NewItem()
        {
            return View("Edit", new Owners());
        }


        [HttpGet]
        public ViewResult Edit(Guid UserId)
        {
            Owners owner = (from p in OwnersRepository.Owners 
                              where p.UserId == UserId select p).First();

          //  id = owner.OwnerId;

            return View(owner);
        }
         
        [HttpPost]
        public ActionResult Edit(Owners owner)
        {
            if (ModelState.IsValid)
            {
                OwnersRepository.SaveOwner(owner);
                TempData["message"] = owner.LastName + " has been saved.";
                return RedirectToAction("Gallery", "Home");
            }
            else // Если ошибка достоверности
                return View(owner);
        }    
    
     }

}